<template>
  <div class="ma-content-block">
    <sa-table ref="crudRef" :options="options" :columns="columns" :searchForm="searchForm">
      <!-- 搜索区 tableSearch -->
      <template #tableSearch>
        <a-col :span="8">
          <a-form-item label="真实姓名" field="real_name">
            <a-input v-model="searchForm.real_name" placeholder="请输入真实姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="用户电话" field="mobile">
            <a-input v-model="searchForm.mobile" placeholder="请输入用户电话" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="注册渠道" field="channel">
            <sa-select v-model="searchForm.channel" dict="register_channel" placeholder="请选择注册渠道" allow-clear />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="登录时间" field="login_time">
            <a-range-picker v-model="searchForm.login_time" :show-time="true" mode="date" />
          </a-form-item>
        </a-col>
      </template>

      <!-- Table 自定义渲染 -->
      <template #status="{ record }">
        <sa-switch v-model="record.status" @change="changeStatus($event, record.id)"></sa-switch>
      </template>
      <template #operationBeforeExtend="{ record }">
        <a-link type="primary" @click="detailAction(record)"> <icon-eye />详情 </a-link>
      </template>
      <!-- 操作后置扩展 -->
      <template #operationAfterExtend="{ record }">
        <a-dropdown trigger="hover" v-if="!isRecovery" @select="selectOperation($event, record)">
          <a-link><icon-double-right /> 更多</a-link>
          <template #content>
            <a-doption value="edit" v-auth="['/app/saiuser/user/AppUser/update']">编辑</a-doption>
            <a-doption value="recharge">充值</a-doption>
            <a-doption value="password">重置密码</a-doption>
          </template>
        </a-dropdown>
      </template>
    </sa-table>

    <!-- 编辑表单 -->
    <edit-form ref="editRef" @success="refresh" />
    <!-- 充值表单 -->
    <recharge-form ref="rechargeRef" @success="refresh" />
    <!-- 详情 -->
    <detail-form ref="detailRef" @success="refresh" />
  </div>
</template>

<script setup>
import { onMounted, ref, reactive, computed } from 'vue'
import { Message, Modal } from '@arco-design/web-vue'
import DetailForm from './detail.vue'
import RechargeForm from './recharge.vue'
import EditForm from './edit.vue'
import api from '../../api/user/user'

// 引用定义
const crudRef = ref()
const editRef = ref()
const detailRef = ref()
const rechargeRef = ref()

let isRecovery = computed(() => (crudRef.value ? crudRef.value.isRecovery : false))

// 搜索表单
const searchForm = ref({
  real_name: '',
  mobile: '',
  channel: '',
  login_time: [],
})

const detailAction = async (record) => {
  detailRef.value?.open(record)
}

const resetPassword = async (id) => {
  api.initUserPassword({ id }).then((res) => res.code === 200 && Message.success(res.message))
}

const changeStatus = async (status, id) => {
  const response = await api.changeStatus({ id, status })
  if (response.code === 200) {
    Message.success(response.message)
    crudRef.value.refresh()
  }
}

const selectOperation = (value, record) => {
  if (value === 'password') {
    Modal.info({
      title: '提示',
      content: '确定将该用户密码重置为 sai123456 吗？',
      simple: false,
      onBeforeOk: (done) => {
        resetPassword(record.id)
        done(true)
      },
    })
    return
  }

  if (value === 'edit') {
    editRef.value?.open('edit')
    editRef.value?.setFormData(record)
    return
  }

  if (value === 'recharge') {
    rechargeRef.value?.open('edit')
    rechargeRef.value?.setFormData(record)
    return
  }
}

// SaTable 基础配置
const options = reactive({
  api: api.getPageList,
  rowSelection: { showCheckedAll: true },
  operationColumnWidth: 200,
  add: {
    show: true,
    auth: ['/app/saiuser/user/AppUser/save'],
    func: async () => {
      editRef.value?.open()
    },
  },
  delete: {
    show: true,
    auth: ['/app/saiuser/user/AppUser/destroy'],
    func: async (params) => {
      const resp = await api.delete(params)
      if (resp.code === 200) {
        Message.success(`删除成功！`)
        crudRef.value?.refresh()
      }
    },
  },
})

// SaTable 列配置
const columns = reactive([
  { title: '用户ID', dataIndex: 'id', width: 100 },
  { title: '头像', dataIndex: 'avatar', type: 'image', size: 48, width: 100 },
  { title: '用户昵称', dataIndex: 'nickname', width: 150 },
  { title: '性别', dataIndex: 'sex', type: 'dict', dict: 'gender', width: 100 },
  { title: '姓名', dataIndex: 'real_name', width: 120 },
  { title: '手机', dataIndex: 'mobile', width: 150 },
  { title: '电子邮箱', dataIndex: 'email', width: 180 },
  { title: '注册渠道', dataIndex: 'channel', type: 'dict', dict: 'register_channel', width: 180 },
  { title: '状态', dataIndex: 'status', type: 'dict', dict: 'data_status', width: 100 },
  { title: '登录IP', dataIndex: 'login_ip', width: 150 },
  { title: '登录时间', dataIndex: 'login_time', width: 180 },
])

// 页面数据初始化
const initPage = async () => {}

// SaTable 数据请求
const refresh = async () => {
  crudRef.value?.refresh()
}

// 页面加载完成执行
onMounted(async () => {
  initPage()
  refresh()
})
</script>
